<template>
  <div class="search-history">
    <van-cell title="搜索记录">
      <div v-if="isDeleteShow">
        <span @click="$emit('update-history', [])">全部删除</span>
        &nbsp;&nbsp;
        <span @click="isDeleteShow = false">完成</span>
      </div>
      <van-icon v-else name="delete-o" @click="isDeleteShow = true" />
    </van-cell>
    <van-cell
      :title="history"
      v-for="(history, index) in searchHistory"
      :key="index"
      @click="onDelete(history, index)"
    >
      <van-icon v-show="isDeleteShow" name="close" />
    </van-cell>
  </div>
</template>

<script>
export default {
  name: 'SearchHistory',
  components: {},
  props: {
    searchHistory: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      isDeleteShow: false
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  // 方法集合
  methods: {
    onDelete(history, index) {
      // 如果是删除状态，则执行删除状态
      if (this.isDeleteShow) {
        return this.searchHistory.splice(index, 1)
        // 持久化处理
        // 1.修改本地存储的数据
        // 2.请求接口删除线上的数据
        // setItem('search-history', this.searchHistory)
      }

      // 非删除状态，执行搜索
      this.$emit('search', history)
    }
  }
}
</script>

<style lang="less" scoped></style>
